# coding:utf-8
# 王一舟
# demo_1.py


'''
deepdiff 是第三方库,需要安装 :  pip install deepdiff
作用:比较python的数据结构(字典,列表,json等)之间的差异
语法结构:DeepDiff("旧参数","新参数")

注意:
1当就参数和新参数一致无差异时,返回的是一个空字典 {}
2参数中值的类型有改变返回: type_changes
3参数中值有改变返回:  values_changed
4参数中值有增加返回: dictionary_item_added
5参数中值有家烧返回: dictionary_item_removed


'''

from deepdiff import DeepDiff

# todo 1 旧参数和新参数无差异时
json_1 = {
    "goods_id": "9",
    "spec": [{"type": "颜色", "value": "粉色"}, {"type": "尺码", "value": "L"}],
    "stock": "7"
}

json_2 = {
    "goods_id": "9",
    "spec": [{"type": "颜色", "value": "粉色"}, {"type": "尺码", "value": "L"}],
    "stock": "7",
}
print(DeepDiff(json_1, json_2))

# todo 2 参数类型有差异 type_changes
json_3 = {
    "goods_id": "9",
    "spec": [{"type": "颜色", "value": "粉色"}, {"type": "尺码", "value": "L"}],
    "stock": 7
}

print(DeepDiff(json_1, json_3))
# 返回{'type_changes': {"root['stock']": {'old_type': <class 'str'>, 'new_type': <class 'int'>, 'old_value': '7', 'new_value': 7}}}
# root 代表的是根节点

# todo 3 参数数值有差异 values_changed

json_4 = {
    "goods_id": "9",
    "spec": [{"type": "颜色", "value": "粉色"}, {"type": "尺码", "value": "L"}],
    "stock": "18"
}
print(DeepDiff(json_1, json_4))
# 返回 {'values_changed': {"root['stock']": {'new_value': '18', 'old_value': '7'}}}


# todo 4 参数的值有增加 'dictionary_item_added'

json_5 = {
    "goods_id": "9",
    "spec": [{"type": "颜色", "value": "粉色"}, {"type": "尺码", "value": "L"}],
    "stock": "7",
    "name": "大斌"
}
print(DeepDiff(json_1, json_5))
#返回{'dictionary_item_added': ["root['name']"]


# todo 5 参数的值有减少 dictionary_item_removed

json_6 = {
    "spec": [{"type": "颜色", "value": "粉色"}, {"type": "尺码", "value": "L"}],
    "stock": "7",
}
print(DeepDiff(json_1, json_6))
#返回{'dictionary_item_removed': ["root['goods_id']"]}


deff = {}

if not deff:
    print("空的")
else:
    print('有的')
